package handler

import (
    "fcs-api/src/app/business"
    "fcs-api/src/app/model"
    "net/http"

    i18n "git.inspur.com/x/common/i18n"
    "github.com/emicklei/go-restful"
)

func (api *APIHandler) handleCreateVersion(request *restful.Request, response *restful.Response) {
    functionInfo, versionInfo, err := validateCreateVersion(request)
    if err != nil {
        handleInternalError(response, err)
        return
    }

    if functionInfo.Runtime != model.CustomContainer {
        err = ossCopy(functionInfo.A5tId, functionInfo.GroupName, functionInfo.FunctionName, model.FunctionLatestVersion, functionInfo.VersionName)
        if err != nil {
            handleInternalError(response, err)
            return
        }
    }

    fcClient, err := GetFcClient(api)
    if err != nil {
        _ = logger.Error("handleCreateVersion: get cmanagerconfig error %v", err)
        handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, i18n.GetMessage(request.Request, "fcs.version.create")))
        return
    }
    var vpcUpdateStatus model.VpcUpdateStatus
    if versionInfo.EnableVpc == 1 {
        vpcUpdateStatus = model.Vpc_Need_Add
    } else {
        vpcUpdateStatus = model.Vpc_No_Need_Update
    }
    updateInfo := &model.UpdateModel{
        FuncInfo:        functionInfo,
        VersionInfo:     versionInfo,
        VpcUpdateStatus: vpcUpdateStatus,
        OldVcpInfo:      nil,
    }
    token := request.HeaderParameter("Authorization")
    functionInfo, err = business.UpdateFunction(fcClient, updateInfo, token)
    if err != nil {
        handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, i18n.GetMessage(request.Request, "fcs.version.create")))
        return
    }
    _ = response.WriteHeaderAndEntity(http.StatusOK, functionInfo)
}

func (api *APIHandler) handleGetVersionList(request *restful.Request, response *restful.Response) {
    logger.Debug("handleUpdateFunction start.")
    from := request.QueryParameter("from")
    functionInfo, err := validateFuncInfo(request)
    if err != nil {
        if from == "apig" {
            _ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"data": []*model.FuncVersion{}})
            return
        }
        handleInternalError(response, err)
        return
    }
    logger.Debug("handleGetVersionList end.")
    versionList, err := business.GetVersionList(functionInfo.Id)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    _ = response.WriteHeaderAndEntity(http.StatusOK, map[string]interface{}{"data": versionList})
}

func (api *APIHandler) handleDeleteVersion(request *restful.Request, response *restful.Response) {
    funcInfo, err := validateDeleteVersion(request)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    fcClient, err := GetFcClient(api)
    if err != nil {
        _ = logger.Error("handleDeleteVersion: get cmanagerconfig error %v", err)
        handleInternalError(response, model.InternalServerError(model.FunctionUpdateError, i18n.GetMessage(request.Request, "fcs.version.delete")))
        return
    }
    token := request.HeaderParameter("Authorization")
    err = business.DeleteVersion(fcClient, funcInfo, token)
    if err != nil {
        handleInternalError(response, err)
        return
    }
    response.WriteHeader(http.StatusOK)
}
